﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;

namespace SusuCMS.Globalization
{
    public class LocalizeDataAnnotationsModelValidatorProvider : DataAnnotationsModelValidatorProvider
    {
        protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
        {
            foreach (var attribute in attributes.OfType<ValidationAttribute>())
            {
                if (!string.IsNullOrWhiteSpace(attribute.ErrorMessage))
                {
                    attribute.ErrorMessage = attribute.ErrorMessage.Localize();
                }
            }

            return base.GetValidators(metadata, context, attributes);
        }
    }
}
